# ------------------------------------------------------------------------------
# Makes all targets for 03_score_diagnositics directory
# Author: Cassidy Shubatt <cshubatt@gmail.com>
# To run: bsub -q big -R "rusage[mem=150000]" make
# ------------------------------------------------------------------------------

# Config -----------------------------------------------------------------------
.ONESHELL:
include config.mk

# Locals -----------------------------------------------------------------------
DIRS = temp/.f log/.f

# Recipes ----------------------------------------------------------------------

## all			: Builds all cohort files
.PHONY: all
all: tested_plots adverse_plots longterm_adverse_plots longterm_adverse_regs \
stress_cath_plots sameday_tn_plots outcome_stats adverse_tbl \
output_tbls output_fgs

## prep			: Prepares cohort for score diagnostics
.PHONY: prep
prep: $(COHORT)
$(COHORT): ../02_prep_and_summarize_cohort/
	$(MAKE) -C ../02_prep_and_summarize_cohort

## tested_plots		: Plots yield and test rates by risk
.PHONY: tested_plots
tested_plots: $(TESTED_PLOTS)
$(TESTED_PLOTS): 01_plot_yield.sh scripts/01_plot_yield.R $(COHORT) $(DIRS)
	bash $<

## adverse_plots		: Plots adverse outcome rates by risk
.PHONY: adverse_plots
adverse_plots: $(ADVERSE_PLOTS)
temp/02_plot_adverse_outcomes/%: 02_plot_adverse_outcomes.sh \
scripts/02_plot_adverse_outcomes.R $(COHORT) $(DIRS)
	bash $<

## longterm_adverse_plots	: Plots longterm adverse outcomes by risk, test
.PHONY: longterm_adverse_plots
longterm_adverse_plots: $(LONGTERM_ADVERSE_PLOTS)
$(LONGTERM_ADVERSE_PLOTS): 03_plot_longterm_adverse.sh \
scripts/03_plot_longterm_adverse.R $(COHORT) $(DIRS)
	bash $<

## longterm_adverse_regs	: Regresses longterm adverse outcomes on risk, test
.PHONY: longterm_adverse_regs
longterm_adverse_regs: $(LONGTERM_ADVERSE_REGS)
$(LONGTERM_ADVERSE_REGS): 04_regress_longterm_adverse.sh \
scripts/04_regress_longterm_adverse.R $(COHORT) $(DIRS)
	bash $<

## stress_cath_plots	: Plots calibration for stress and cath tests
.PHONY: stress_cath_plots
stress_cath_plots: $(STRESS_CATH_PLOTS)
$(STRESS_CATH_PLOTS): 05_plot_stress_cath_calib.sh \
scripts/05_plot_stress_cath_calib.R $(COHORT) $(DIRS)
	bash $<

## sameday_tn_plots	: Plots outcome rates by level of sameday tn
.PHONY: sameday_tn_plots
sameday_tn_plots: $(SAMEDAY_TN_PLOTS)
$(SAMEDAY_TN_PLOTS): 06_sameday_tn_plots.sh scripts/06_sameday_tn_plots.R \
$(COHORT) $(DIRS)
	bash $<

## adverse_tbl		: Builds table of adverse rates for appendix
.PHONY: adverse_tbl
adverse_tbl: $(ADVERSE_TBL)
$(ADVERSE_TBL): 07_adverse_event_tbl.sh scripts/07_adverse_event_tbl.R\
$(COHORT) $(DIRS)
	bash $<

## outcome_stats		: Reports various outcome stats cited in paper
.PHONY: outcome_stats
outcome_stats: $(OUTCOME_STATS)
$(OUTCOME_STATS): 08_report_outcome_stats.sh \
scripts/08_report_outcome_stats.R $(COHORT) $(DIRS)
	bash $<

## output_tbls		: Copies tables used in paper to `output` directory
.PHONY: output_tbls
output_tbls: $(OUTPUT_TBLS)
$(OUTPUT_DIR)/tables/%.tex : %.tex
	cp $< $@

## output_fgs		: Copies tables used in paper to `output` directory
.PHONY: output_fgs
output_fgs: $(OUTPUT_FGS)
$(OUTPUT_DIR)/figures/%.png : %.png
	cp $< $@

# Helper Recipes ---------------------------------------------------------------
.PHONY: dirs
dirs: $(DIRS)
%/.f :
	mkdir -p $(dir $@)
	touch $@

## clean			: Clears temp file folder and log files.
.PHONY: clean
clean: $(DIRS)
	rm -rf temp/*
	rm -f log/*

## style			: Styles R scripts according to tidyverse guidelines
.PHONY: style
style:
	source ~/anaconda3/etc/profile.d/conda.sh
	conda activate stressr
	R --vanilla --silent -e "styler::style_dir(path = 'scripts', filetype = c('R', 'Rmd', 'Rprofile'))"

## help			: Prints available .PHONY targets with description
.PHONY : help
help : Makefile
	@sed -n 's/^##//p' $<
